<template>
	<view>
		<uni-nav-bar :border="false" :fixed="true" :statusBar="true">
			<view class="flex align-center justify-center font-weight-bold w-100" >
			
				<view v-for="(item,index) in tabBars" @click="changeTab(index)" :key="index" class="mx-2" :class="tabBarIndex===index?'font text-main':'font-sm text-light-muted'">{{item.name}}</view>
			</view>
		</uni-nav-bar>
		<swiper :current="tabBarIndex" @change="onChangeTab" :duration="150" :style="'height:'+scrollH+'px;'">
		  	<swiper-item v-for="(item,index) in newList" :key="index">
				<scroll-view scroll-y="true":style="'height:'+scrollH+'px;'" @scrolltolower="loadMore(item)">
					<block v-for="(item2,index2) in item.list" :key="index2">
					        <common-list :item="item2" :index="index2">
							</common-list>
							<divider></divider>
					    </block>
						<load-more :loadmore="item.loadmore"></load-more>
				</scroll-view>
			</swiper-item>
			<swiper-item>
				<scroll-view scroll-y="true":style="'height:'+scrollH+'px;'"></scroll-view>
				
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
		import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue';
	    import commonList from '@/components/common/common-list.vue';
	    import loadMore from '@/components/common/load-more.vue';
	    import { mapState } from 'vuex';
	export default {
		components:{
			uniNavBar,commonList,loadMore
		},
		data() {
			return {
			  scrollH:500,
			  tabBarIndex:0,
			  tabBars:[{
				  name:'创建的活动'
			  },{
				 name:'参加的活动'  
			  }],
			  newList:[]
			 	
			}
		},
		computed:{
			...mapState({
				loginStatus:state=>state.loginStatus
			})
		},
		onLoad() {
		    uni.getSystemInfo({
		    	success: (res) => {
		    		this.scrollH = res.windowHeight - res.statusBarHeight-44
		    	}
		    }),
			uni.$on('updateIndex',()=>{
				this.getData()
			}),
			this.getData()
		},
		onUnload() {
			uni.$off('updateIndex',(e)=>{})
		},
		methods: {
			changeTab(index){
				this.tabBarIndex=index;
				 this.getList()
			},
			loadMore(e){
			    console.log(e)
				if(e.loadmore !== "上拉加载更多") return;
				e.loadmore ="加载中..."
                e.pagnation.pageNum++
                this.getList();
			},
			onChangeTab(e){
				this.tabBarIndex = e.detail.current;
				
			},
			getData(){
				var arr=[]
				for(let i =0;i<this.tabBars.length;++i)
				{
					let obj={
						list:[],
						loadmore:'上拉加载更多',
						pagnation:{
							pageNum:1,
							pageSize:10
						},
						
					}
					arr.push(obj)
				}
				this.newList =arr
		         if(this.tabBars.length>0)
				 {
					 this.getList()
				 }
			},
			getList(){
				if(this.loginStatus){
					let userInfo = JSON.parse(uni.getStorageSync("userInfo"));
					let index = this.tabBarIndex;
					let data={}
					if(index == 0){
					   data={
					  	...this.newList[index].pagnation,
					  	createId:userInfo.userId
					  }	
				
					}else{
						 data={
							...this.newList[index].pagnation,
							joinedId:userInfo.userId
						}	
					}
					this.$R.get('/meet/event/listToken',data).then((res)=>{
					   this.newList[index].list = this.newList[index].pagnation.pageNum==1?res.rows:[...res.rows,...this.newList[index].list],
					   this.newList[index].loadmore = res.rows.length<10?'没有更多了':'上拉加载更多'				
					})
				
				}else{
					 return;	
				}
				
			}
			
		}
	}
</script>

<style>

</style>
